package com.moore.thread;

import com.moore.Servlet.*;
import com.moore.net.HttpRequest;
import com.moore.net.HttpResponse;

import java.io.IOException;
import java.net.Socket;

public class SocketThread extends Thread{
    private final Socket socket;
    private HttpRequest httpRequest;
    private HttpResponse httpResponse;

    public SocketThread(Socket socket) {
        this.socket = socket;
    }

    public void run() {
        if (socket != null){
            httpRequest = new HttpRequest(socket);
            httpResponse = new HttpResponse(httpRequest, socket);
        }
        // 根据文件内容或路径进行操作
        byte[] fileData;
        FileServlet fileServlet = new FileServlet();
        fileData = fileServlet.doGet(httpRequest, httpResponse);
        if (fileData == null){
            switch (httpRequest.getUrl()) {
                case "/login":
                    LoginServlet loginServlet = new LoginServlet();
                    fileData = loginServlet.doGet(httpRequest, httpResponse);
                    break;
                case "/register":
                    RegServlet regServlet = new RegServlet();
                    fileData = regServlet.doGet(httpRequest, httpResponse);
                    break;
                case "/index":
                    UserServlet userServlet = new UserServlet();
                    fileData = userServlet.doGet(httpRequest, httpResponse);
                    break;
                case "/goods":
                    GoodsServlet goodsServlet = new GoodsServlet();
                    fileData = goodsServlet.doGet(httpRequest, httpResponse);
                    break;
                case "/password":
                    PasswordServlet passwordServlet = new PasswordServlet();
                    fileData = passwordServlet.doGet(httpRequest, httpResponse);
                    break;
                case "/balance":
                    BalanceServlet balanceServlet = new BalanceServlet();
                    fileData = balanceServlet.doGet(httpRequest, httpResponse);
                    break;
                case "/orders":
                    OrdersServlet ordersServlet = new OrdersServlet();
                    fileData = ordersServlet.doGet(httpRequest, httpResponse);
                    break;
                case "/address":
                    AddressServlet addressServlet = new AddressServlet();
                    fileData = addressServlet.doGet(httpRequest, httpResponse);
                    break;
                case "/UserForAdmin":
                    UserForAdminServlet userForAdminServlet = new UserForAdminServlet();
                    fileData = userForAdminServlet.doGet(httpRequest, httpResponse);
                    break;
                case "/upData":
                    FileUploadServlet fileUploadServlet = new FileUploadServlet();
                    fileData = fileUploadServlet.doGet(httpRequest, httpResponse);
                    break;
                case "/goodsForAdmin":
                    GoodsForAdminServlet goodsForAdminServlet = new GoodsForAdminServlet();
                    fileData = goodsForAdminServlet.doGet(httpRequest, httpResponse);
                    break;
                case "/comment" :
                    CommentServlet commentServlet = new CommentServlet();
                    fileData = commentServlet.doGet(httpRequest, httpResponse);
                    break;
                case "/support" :
                    SupportServlet supportServlet = new SupportServlet();
                    fileData = supportServlet.doGet(httpRequest, httpResponse);
                    break;
                case "/userInfo" :
                    UserInfoServlet userInfoServlet = new UserInfoServlet();
                    fileData = userInfoServlet.doGet(httpRequest, httpResponse);
                    break;
                default:
                    NotFoundServlet notFoundServlet = new NotFoundServlet();
                    fileData = notFoundServlet.doGet(httpRequest, httpResponse);
                    break;
            }
        }
        // 运行响应方法（传值）
        httpResponse.responseLine();
        httpResponse.responseHeader(fileData);
        httpResponse.responseBlankLine();
        httpResponse.responseBody(fileData);
        // 关闭连接
        try {
            if (socket != null) {
                socket.close();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
